/*
 * Copyright (c) 2018-2023 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#include "subcmd.h"

static void show_usage(const char *);

/**
 * @brief       qp js showconfig handler, to query parameter settings from js.
 *
 * @param[in]   argc          #1: number of input argument
 * @param[in]   argv          #2: string array of the input argument
 *
 * @retval      0             #1: succeed
 * @retval      1             #2: input option is wrong or kind of input argument not specified
 * @retval      2             #3: argument has syntax error
 * @retval      3             #4: failed to query parameter settings
 * @retval      4             #5: not enough memory to print json string
 */
int
cmd_show_settings(int argc, char **argv)
{
    bool_t showAll = FALSE;
    while (TRUE) {
        int option_index = 0;
        static struct option long_options[] = {
            {"help", no_argument, 0, 'h'},
            {"all", no_argument, 0, 'a'},
            {NULL, 0, 0, 0}
        };

        int opt;
        opt = getopt_long(argc, argv, "ha", long_options, &option_index);
        if (-1 == opt) {
            break;
        }

        switch (opt) {
        case 'a':
            showAll = TRUE;
            break;
        case 'h':
            show_usage(argv[0]);
            exit(0);
        case '?':
        default :
            show_usage(argv[0]);
            exit(1);
        }
    }

    struct QuickPoolParameter *settings;
    settings = client_get_setting(NULL);
    if (NULL == settings) {
        exit(3);
    }

    unsigned int i;
    for (i=0; i<settings->number; i++) {

        if (NULL == settings->param[i].param_name
            || strcmp(settings->param[i].param_name, "") == 0) { // no name

            continue;
        }

        if (!showAll && 0 > settings->param[i].index) { // parameter is not set value
            continue;
        }

        switch (settings->param[i].param_type) {
        case STRING_PARAM:

            if (NULL == settings->param[i].string_value
                || strcmp(settings->param[i].string_value, "") == 0) {

                break;
            }

            fprintf(stdout, "%s = %s\n", settings->param[i].param_name,
                    settings->param[i].string_value);
            break;
        case INT_PARAM:

            fprintf(stdout, "%s = %d\n", settings->param[i].param_name,
                    settings->param[i].int_value);
            break;
        case BOOL_PARAM:

            if (settings->param[i].bool_value) {
                fprintf(stdout, "%s = Y\n", settings->param[i].param_name);
            } else {
                fprintf(stdout, "%s = N\n", settings->param[i].param_name);
            }

            break;
        case FLOAT_PARAM:

            fprintf(stdout, "%s = %.2f\n", settings->param[i].param_name,
                    settings->param[i].float_value);
            break;
        }
    }

    exit(0);
} // end function cmd_show_settings

static void
show_usage(const char *cmd)
{
    fprintf(stdout, "qp js %s usage: [-h, --help] [-a, --all]\n", cmd);

    return;
}
